/**
 * 司机列表
 *
 */
define(["app", "comm/pager/PagerControl", "js/services/MessageBox", "js/services/MuseService", "js/directives/DatetimePicker"], function (app) {

    var deps = ["$scope", "$state", "$modal", "MessageBox", "MuseService"];

    function controller ($scope, $state, $modal, MessageBox, MuseService) {
        //初始化查询对象 和 分页对象
        app.regsiterCacheData($scope, $state);
        $scope.queryObj = $scope.queryObj || {};
        $scope.pagerObj = $scope.pagerObj || {};

        //获取列表
        $scope.getObjList = function (pageNo, pageSize) {
            var curPage = pageNo ||$scope.pagerObj.currentPage ||1;
            var param = {
                page: curPage, pageSize: pageSize || $scope.pagerObj.pageSize,
                param: {
                    username: $scope.queryObj.username || "",
                    nickname: $scope.queryObj.nickname || "",
                    authorized: $scope.queryObj.authorized || "",
                    expireState: $scope.queryObj.expireState || "",
                    createTimeStart: $scope.queryObj.createTimeStart || "",
                    createTimeEnd: $scope.queryObj.createTimeEnd || ""
                }
            };

            MuseService.postObj(baseServices_restfulUrl + "driver/search.json", param).success(
                function (data, status, headers, config) {
                    // console.log(data);
                    if (data.state == 0) {
                        var page = data.data;
                        $scope.driverList = page.data;

                        //分页器
                        $scope.pagerObj = PageCtrl().page($scope.pagerObj, page.total, curPage);
                    }
                })
            .error(function () {
                MessageBox.error('服务器正在开小差zZZ,获取不到数据');
            });
        };

        $scope.getObjList();

        //新增
        $scope.add = function () {
            $state.go('driverEdit');
        };

        //删除
        $scope.delete = function (driver) {
            var dialog = MessageBox.confirm('确认要删除该信息吗？');
            dialog.result.then(function () {
                MuseService.post(baseServices_restfulUrl + 'driver/delete.json', {id: driver.id}).success(
                    function (data) {
                        if (data.state == 0) {
                            MessageBox.notify({msg: '删除成功！', displayTime: 1000});
                            $scope.getObjList();
                        } else {
                            MessageBox.error('删除异常：' + data.message || "服务器正在开小差");
                        }
                    })
                .error(function () {
                    MessageBox.error('删除失败：服务器正在开小差');
                });
            });
        };

        //编辑
        $scope.edit = function (id) {
            $state.go('driverEdit', {id: id});
        };

        //查看车辆
        $scope.getCar = function (id) {
            $state.go('driverCarList', {driverId: id});
        };

        /**
         * 审核
         * @param driverId
         * @param boolean
         */
        $scope.authorized = function (driverId, authorized) {
            var dialog = MessageBox.confirm('确认要通过审核吗？');
            dialog.result.then(function () {
                MuseService.post(baseServices_restfulUrl + 'driver/authorized.json', {
                    driverId: driverId,
                    authorized: authorized
                }).success(
                    function (data) {
                        if (data.state == 0) {
                            MessageBox.notify({msg: '审核成功！', displayTime: 1000});
                            $scope.getObjList();
                        } else {
                            MessageBox.error('操作异常：' + data.message || "服务器正在开小差");
                        }
                    })
                .error(function () {
                    MessageBox.error('审核失败：服务器正在开小差');
                });
            });
        };

        /**
         * 导出到Excel
         */
        $scope.exportDrivers = function () {
            // MuseService.post(baseServices_restfulUrl + 'driver/exportDrivers.json', {}).success(function (data) {
            //     if (data.code == undefined) {   //如果流程正常，返回的data是一个文件流
            //         window.location.href = baseServices_restfulUrl + 'driver/exportDrivers.json';
            //         MessageBox.notify("导出成功！");
            //     } else {
            //         MessageBox.error('错误：' + data.msg);
            //     }
            // }).error(function (data) {
            //     MessageBox.error('操作异常：' + data.message || "服务器正在开小差");
            // });
            window.location.href = baseServices_restfulUrl + 'driver/exportDrivers.json';
            MessageBox.notify("导出成功！");
        };

        $scope.pageChanged = function () {
            PageCtrl().check($scope.pagerObj);
            $scope.getObjList($scope.pagerObj.currentPage, $scope.pagerObj.pageSize);
        }
    }

    controller.$inject = deps;
    app.lazy.controller("DriverListController", controller);
});
